home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5142 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  75 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: simple design question
  5. Date: 02 Feb 1996 18:41:41 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Feb2194144@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <DM4u8I.G1H@emr1.emr.ca>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: jagrant@emr1.emr.ca's message of Fri, 2 Feb 1996 05:07:30 GMT
  12.  
  13. In article <DM4u8I.G1H@emr1.emr.ca> jagrant@emr1.emr.ca (John Grant) writes:
  14.  
  15.    This is a design question, not a syntax question, so forgive the pseudo-
  16.    code.
  17.  
  18.    I'm trying to design/write a class for complex file I/O (I'll state it
  19.    in simple terms here).
  20.  
  21.    I want to use it like this:
  22.        object.Open("xxx")
  23.        for(...){
  24.          object.Read(stuff);
  25.        }
  26.        object.Close();
  27.  
  28.    But I want to also have a single function that will Open/Read/Close in a
  29.    single operation:
  30.        object.GetData("xxx",stuff);
  31.  
  32.    The simple question is: should I implement GetData() as part of this
  33.    class, i.e.:
  34.        int myclass::GetData(...){
  35.        {    Open()
  36.            Read()
  37.            Close()
  38.        }
  39.  
  40.    or should GetData() really be in a separate class which uses the first class:
  41.        int myclass2::GetData(...)
  42.        { myclass *object=new myclass;
  43.            object->Open(...)
  44.            object->Read(...)
  45.            object->Close(...);
  46.            delete object;
  47.        }
  48.  
  49.    If so, is that the best way to do it, or is there a slicker C++ way to do
  50.    that using inheritance etc (which I am just trying to figure out).
  51.  
  52.    The reason I ask, is the potential for conflict in the use of the
  53.    private variables. For example, internally there might be:
  54.        private:    FILE *f;
  55.                <...more internals...>
  56.  
  57.    If I used it incorrectly, i.e.:
  58.        object.Open(...)
  59.        object.GetData(...)    //does Open/Read/Close
  60.        object.Read(...)
  61.    then object.Read() would break because GetData() calls Close(), invalidating
  62.    'f' and other internals.
  63.  
  64. There is no problem with this approach as long the effects of the member-
  65. functions are documented. For example 'GetData' removes the connection to
  66. the stream and 'Read' operates on an object that is connected to a stream.
  67. Thus the sequence of method-invocations 'Open,GetData,Read' is not valid
  68. according to the semantics of the member-functions. In a robust program
  69. some sort of error-handling should cover such situations.
  70. Anyway I tend to your proposed solution to make 'GetData' an indendent
  71. function. Following this idea you can develop the system in layers which
  72. has been proven as quite useful technique.
  73.  
  74.         Enno
  75.